<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">区域名称：</label>
                                    <div class="layui-input-inline">
                                        <input name="name" class="layui-input" type="text" placeholder="请输入区域名称"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">区域编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入区域编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">是否启用：</label>
                                    <div class="layui-input-inline">
                                        <select name="isDeleted">
                                            <option value="">请选择是否启用</option>
                                            <option value="1">启用</option>
                                            <option value="0">停用</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="pTime" class="layui-input date-icon" type="text"
                                               placeholder="选择时间" style="width: 200px" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" type="hidden"/>
                                    <input name="endCreateTime" id="eTime" type="hidden"/>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" layui-form-keyDownSearch="true" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="robotAreaBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="robotAreaTable" lay-filter="robotAreaTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="robotAreaTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="robotAreaForm">
    <form lay-filter="robotAreaForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">区域名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入区域名称" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">区域编号</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入区域编号" type="text" class="layui-input" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属地图</label>
            <div class="layui-input-block">
                <select name="mapId" id="mapId" lay-filter="mapId" lay-verify="required"
                        required></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">区域类型</label>
            <div class="layui-input-block">
                <select name="type">
                        <option value="logistics">物流区域</option>
                        <option value="production">生产区域</option>
                        <option value="warn">红线区域</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否启用</label>
            <div class="layui-input-block">
                <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false" lay-text="启用|停用"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <input name="remark" placeholder="请输入描述" type="text" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="robotAreaFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        //时间范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    var dateValue = value.split(" - ");
                    $("#sTime").val(dateValue[0] + " 00:00:00");
                    $("#eTime").val(dateValue[1] + " 23:59:59");
                } else {
                    $("#sTime").val();
                    $("#eTime").val();
                }
            }
        });
        
        // 渲染表格
        var insTb = table.render({
            elem: '#robotAreaTable',
            url: config.base + 'enterprise/robot/admin/robotArea/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {field: 'number', title: '序号',type: 'numbers',width: 60 }
                ,{field: 'code', align:'center', title: '区域编号'}
                ,{field: 'name', align:'center', title: '区域名称'}
                ,{field: 'remark', align:'center', title: '区域描述'}
                ,{field: 'typeDesc', align:'center', title: '区域类型'}
                ,{field: 'mapId', align:'center', title: '所属地图',templet:function(d){
                        let map = '';
                        admin.reqSync('enterprise/robot/admin/robotMap/list', {id: d.mapId}, function (res) {
                            if (200 == res.code&&res.data.length!=0) {
                                map =  res.data[0].name;
                            }
                        }, 'GET', true);
                        return map;
                    }
                 }
                ,{field: 'isDeleted',  title: '是否启用',templet:function (d) {
                        if(d.isDeleted){
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" checked="'+d.isDeleted+'" lay-filter="isDeleted"  lay-text="启用|停用"/>';
                        }else{
                            return '<input name="isDeleted" type="checkbox" lay-skin="switch" value="'+d.id+'" lay-filter="isDeleted"   lay-text="启用|停用"/>';
                        }
                    }}
                ,{field: 'creatorName', align:'center', title: '添加人'}
                ,{field: 'createTime', align:'center', title: '添加时间'}
                ,{align: 'center', toolbar: '#robotAreaTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {

            }
        });

        // 工具条点击事件
        table.on('tool(robotAreaTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(robotAreaTable)', function(obj){
            table.reload('robotAreaTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#robotAreaBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '660px',
                offset: ['165px','665px'],
                title: data ? '修改调度区域' : '添加调度区域',
                content: $('#robotAreaForm').html(),
                success: function () {
                    if(!data){
                        admin.req('enterprise/robot/admin/robotMap/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择地图", "mapId", 0);
                            }else {
                                layer.msg('获取地图失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                    }
                    if(data){
                        admin.reqSync('enterprise/robot/admin/robotMap/list', {limit: 9999}, function (res) {
                            if (200 == res.code) {
                                loadSelectHtml(res.data, "请选择地图", "mapId", 0);
                            }else {
                                layer.msg('获取地图失败', {icon: 2, time: 1000});
                            }
                        }, 'GET', true);
                    }

                    if (data){
                        var option = $("form select[name='type']").find("option");
                        layui.each(option, function(index, item){
                            if(data.type  == item.text){
                                data.type = item.value;
                            }
                        });
                    }

                    form.val('robotAreaForm', data);
                    // 表单提交事件
                    form.on('submit(robotAreaFormSubmit)', function (d) {
                        layer.load(2);
                        admin.req(data ? 'enterprise/robot/admin/robotArea/update' : 'enterprise/robot/admin/robotArea/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('robotAreaTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }


        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        form.on('switch(isDeleted)',function(obj){
            if(obj.elem.checked){
                enable(obj.value)
            }else{
                disable(obj.value)
            }
        })
        //是否启用
        function enable(id){
            admin.req('enterprise/robot/admin/robotArea/enable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('robotAreaTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        //是否停用
        function disable(id){
            admin.req('enterprise/robot/admin/robotArea/disable', {id:id}, function (res) {
                if (res.code == 200) {
                    layer.msg(res.msg, {icon: 1});
                    insTb.reload('robotAreaTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'POST');
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/robot/admin/robotArea/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type === 0) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type === 1) {
                    html += `<option value='${data[i].id}'>${data[i].corporateName}</option>`;
                }
            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }

    });
</script>